<?php

class UserModel extends BaseModel
{

    var $table  = 'users';	

    var $_page_rows = 20;	
	function moneyuser($uid='')
	{
		if($uid=='')$uid=$GLOBALS['SCup']->SCup_user_msg['user_id'];

        $info = $this->S_Sql_Exe(array('fields' =>'money','conditions' =>"user_id = '{$uid}'",'limit' =>'1','return' =>'one'));
		return $info['money'];
	}	

	function moneyadd($money,$uid)
	{
		$sql="update ".$this->_prefix."users set money = money + $money where user_id = ".$uid;
		return $query = $this->db->query($sql); 
	}

	function moneypay($money,$uid,$shopmoney=0)
	{
			
		$sql="update ".$this->_prefix."users set money = money - $money,totalpay = totalpay + $money,shopmoney = shopmoney + $shopmoney where user_id = ".$uid;
		return $query = $this->db->query($sql); 

		
	}

	function moneylog($ary){

		return $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'usermoney','data' => $ary, 'mode' => 'INSERT'));
	}	


	function addmoney($ary){

		return $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'addmoney','data' => $ary, 'mode' => 'INSERT'));
	}	



	function mailCron($ary){
		$keys=$values='';
		foreach($ary as $i => $valuez){
			$a=$i=='addtime'?"":',';
			$keys.='`'.$i.'`'.$a;
			$values.='\''.$valuez.'\''.$a;
		}
		$sql='insert into '.$this->_prefix.'cron ('.$keys.') VALUES ('.$values.')';
		$this->db->query($sql);
	}


	function ticketCreate($userid,$productid,$orderid,$groupid)
	{
		$ticketNumber=$orderid.rand('100','999');
		$ticketPassword=rand('100000','999999');
		$ary=array(
			'uid'=> $userid,
			'groupid'=>$groupid,
			'productid'=>$productid,
			'orderid'=>$orderid,
			'number'=>$ticketNumber,
			'password'=>$ticketPassword,
			'status'=> 1
		);

		return $this->IU_Sql_Exe(array ('table'=>$this->_prefix.'ticket','data' => $ary, 'mode' => 'INSERT'));

	}

	function ticketmy($type='')
	{

        $userid=$GLOBALS['SCup']->SCup_user_msg['user_id'];
       

		if($type!='')
		{
           $conditions = 't.uid = '.$userid .' and g.display =1 and t.`status` = '.intval($type);

		}else
		{
			$conditions = ' t.uid = '.$userid.' and g.display = 1 ';	

		}

	     $table = $this->_prefix.'ticket t LEFT JOIN '.$this->_prefix.'product'.' p ON t.productid = p.id left join '.$this->_prefix.'group g on p.groupid = g.id ';
	  

		 $info['page'] = $this->Page(array('table'=>"{$table}",'conditions' =>"{$conditions}",'url' => '?app=user&act=ticket&type='.$type));		
		 
		 $info['data'] = $this->S_Sql_Exe(array('table'=>"{$table}",'fields'=> ' t.*,p.name,p.img,g.intro,g.perioddate,g.name as groupname ','conditions' =>"{$conditions}",'limit' =>$info['page']['sql_limit']));
		 
		 return $info;
	}









	function showmoneylog($userid)
	{
  
	     $table = $this->_prefix.'usermoney';
	     $conditions =  'userid = '.$userid;

		 $info['page'] = $this->Page(array('table'=>"{$table}",'conditions' =>"{$conditions}",'url' => '?app=user&act=money'));		
		 
		 $info['data'] = $this->S_Sql_Exe(array('table'=>"{$table}",'fields'=> ' * ','conditions' =>"{$conditions}",'order' =>'mid desc','limit' =>$info['page']['sql_limit']));

        return $info;

	}




	function getticket($id,$seller=0)
	{
       $userid=$GLOBALS['SCup']->SCup_user_msg['user_id'];

		if($seller==0)
		{
	
			$sql='select t.*,p.name,p.img,g.intro,g.name as groupname,p.price,p.nowprice,p.bookprice,s.sellername,s.sellerurl,g.perioddate,s.selleraddress from '.$this->_prefix.'ticket t LEFT JOIN '.$this->_prefix.'product p on t.productid = p.id left join '.$this->_prefix.'seller s on p.sellerid=s.id LEFT JOIN '.$this->_prefix.'group g on t.groupid = g.id   where  t.uid = '.$userid .' and t.ticketid = '.intval($id);
		}else
		{
			$sql='select t.*,p.name,p.img,g.intro,g.name as groupname,p.price,p.nowprice,p.bookprice,s.sellername,s.sellerurl,g.perioddate,s.selleraddress from '.$this->_prefix.'ticket t LEFT JOIN '.$this->_prefix.'product p on t.productid = p.id left join '.$this->_prefix.'seller s on p.sellerid=s.id LEFT JOIN '.$this->_prefix.'group g on t.groupid = g.id   where  t.ticketid = '.intval($id);	
		}

        $result = $this->db->get_one($sql);
		return $result;
	}



}




?>